<?
defined( '_VALID_MOS' ) or die( 'Доступ ограничен' );

$_MAMBOTS->registerFunction( 'onPrepareContent', 'tvAutoUpdate' );

function tvAutoUpdate( $published, &$row, &$params, $page=0 ) {
	global $database, $_MAMBOTS;
	global $mosConfig_absolute_path, $mosConfig_live_site,$mosConfig_offset;

require_once($mosConfig_absolute_path."/administrator/components/com_tv/sub_update.php");
require_once($mosConfig_absolute_path."/administrator/components/com_tv/config.tv.php");
$beg=time();
$mostime=time()+($mosConfig_offset*60*60);
$today=getdate($mostime);
$dayofweek = $today['wday']; 
$dayofweek=$dayofweek?$dayofweek:7;

$newprog=1;

if($autoup)
{
  if(file_exists($mosConfig_absolute_path."/administrator/components/com_tv/run.tmp")) //Мамбот уже запущен!
       return;
 $ftmp = fopen($mosConfig_absolute_path."/administrator/components/com_tv/run.tmp", "w");
 fputs($ftmp, $mostime);
 fclose($ftmp);
ignore_user_abort(TRUE); 
@ini_set("max_execution_time","600");

if(file_exists($mosConfig_absolute_path."/administrator/components/com_tv/lastcheck.tmp"))
   {
    $check=file($mosConfig_absolute_path."/administrator/components/com_tv/lastcheck.tmp");
    if(isset($check[0]))
        $lastcheck = $check[0]; 
    else
        $lastcheck = "";
    if(isset($check[1]))
        $lastday = $check[1]; 
    else
        $lastday = 0;
   }
else
  {
    $lastcheck = "";
    $lastday = 0;
 }

if($lastday<$mostime)
{
$week_name=GetKulichkiWeekName();
$wn=time();
if($lastcheck!=$week_name)
{
GetKulichkiTV($week_name);
$newprog=GetProgram($mosConfig_absolute_path."/administrator/components/com_tv/program/");
$end=time();
}
if($newprog)
 {
 $fp = fopen($mosConfig_absolute_path."/administrator/components/com_tv/lastcheck.tmp", "w");
 fputs($fp, $week_name);
 fputs($fp,"\r\n".mktime(0,0,0,$today['mon'],$today['mday']-$dayofweek+7,$today['year']));
 fputs($fp, "\r\n".$newprog);
 fputs($fp, "\r\n".($wn-$beg));
 fputs($fp, "\r\n".($end-$beg));
 fclose($fp);
}
}
}
if(file_exists($mosConfig_absolute_path."/administrator/components/com_tv/run.tmp"))
 unlink($mosConfig_absolute_path."/administrator/components/com_tv/run.tmp");
}
?>

